<template>
  <div class="common">

    <div class="form-box">
      <el-form ref="form" :model="pageParams" label-width="90px">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="员工姓名:">
              <el-input v-model="pageParams.name" placeholder="请输入员工姓名" size="small" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工账号:">
              <el-input v-model="pageParams.account" placeholder="请输入员工账号" size="small" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="工作模式:">
              <el-select
                v-model="pageParams.workPatternId"
                placeholder="请选择工作模式"
              >
                <el-option
                  v-for="item in workPatternList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="月份:">
              <!-- <el-input v-model="pageParams.workEndMinute1" placeholder="请选择月份" size="small" /> -->
              <el-date-picker
                v-model="pageParams.month"

                type="month"
                placeholder="请选择月份"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属机构:">
              <el-select
                v-model="pageParams.agencyId"
                placeholder="请选择所属机构"
                style="width:100%"
                clearable
              >
                <el-tree
                  node-key="id"
                  :data="treeData"
                  class="add_tree"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :check-on-click-node="true"
                >
                  <span slot-scope="{data}" class="custom-tree-node">
                    <el-option
                      style="padding: 0"
                      :label="data.name"
                      :value="data.id"
                    /></span></el-tree>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="searchStaff">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-col>

        </el-row>
      </el-form>
    </div>

    <!-- Table 表格 -->
    <div class="table">
      <!-- 放置table组件 -->
      <el-button type="primary" @click="$router.push('/driverManagement/schedulingInstall')">排班设置</el-button>
      <el-button @click="bindingArrange">绑定排班</el-button>

      <el-table :data="list" @select="onselect">
        <!-- 放置列 -->
        <el-table-column
          type="selection"
          width="45"
        />
        <el-table-column width="110" prop="employeeNumber" align="center" label="员工账号" />
        <el-table-column width="110" prop="name" align="center" label="员工姓名" />
        <el-table-column width="110" prop="phone" align="center" label="员工电话" />
        <el-table-column width="110" prop="userType" align="center" label="员工角色">
          <template v-slot="{row}">
            <span>{{ row.userType===1?'员工':row.userType===2 ? '快递员':'司机' }}</span>
          </template>
        </el-table-column>
        <el-table-column width="110" prop="workPatternName" align="center" label="当前工作模式" />

        <el-table-column
          v-for="(item,index) in workSchedulesList.length
            ? workSchedulesList[0].workSchedules
            :[]"
          :key="item.index"
          :label="String(index+1)"
          align="center"
          width="70"
        >
          <template v-slot="{row}">
            <div>
              {{ row.workPatternName?row.workSchedules[index] ? '上' : '休': '无' }}
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <!-- 放置分页组件 -->
    <el-row type="flex" style="height:60px" align="middle" justify="center">
      <!-- 放置分页组件 -->
      <!-- 分页器 -->
      <div class="block">
        <el-pagination
          :current-page="pageParams.page"
          :page-size="pageParams.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>

    </el-row>
    <!-- 排班绑定 -->
    <el-dialog
      :visible="dialogVisible"
      title="人工调整"
      width="400px"
    >
      <el-radio-group v-model="radioValue">
        <el-radio label="option1">礼拜制</el-radio>
        <el-radio label="option2">连续制</el-radio>
      </el-radio-group>

      <br>

      <el-select v-model="selectedValue" placeholder="请选择">
        <el-option
          v-for="item in workPatternList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>

      <div style="text-align: right; margin-top: 20px">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="confirmDialog">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getOrganizationAPI, getSchedulingAPI, getWorkPatternListAPI } from '@/api/staffManagement'
export default {
  data() {
    return {
      dialogVisible: false,
      radioValue: 'option1',
      selectedValue: '',
      total: 100,
      list: [],
      // 获取列表   模糊搜索
      pageParams: {
        account: '', // 员工 账号
        name: '', // 名字
        month: '',
        workPatternId: '', // 工作模式
        agencyId: '', // 机构ID
        page: 1, // 页码
        pageSize: 10 // 每页条数
      },
      workSchedulesList: [], // 工作时间计划表
      // 机构树形控件数据
      defaultProps: {
        label: 'name',
        children: 'children',
        value: '',
        emitPath: false
      },
      treeData: [],
      label: '',
      selectOption: '',
      form: {
        account: '',
        agentId: -1 && '',
        name: '',
        phone: ''
      },
      workPatternList: [], // 工作模式列表
      binding: []

    }
  },
  created() {
    this.getScheduling()
    this.organizationChange()
    this.getWorkPatternList()
  },
  methods: {
    // 获取员工员列表
    async getScheduling() {
      const res = await getSchedulingAPI(this.pageParams)
      this.list = res.items
      this.workSchedulesList = res.items || []
      this.total = res.counts
    },
    // 获取树状机构信息
    async organizationChange() {
      const res = await getOrganizationAPI()
      this.treeData = JSON.parse(res)
    },

    // 搜索
    searchStaff() {
      this.pageParams.page = 1
      this.pageParams.pageSize = 10
      this.getScheduling()
    },
    // 重置
    reset() {
      this.pageParams = {
        account: '', // 员工 账号
        name: '', // 名字
        workPatternId: '', // 机构ID
        workEndMinute1: '', // 工作模式
        agentId: '', // 月份 假的
        page: 1, // 页码
        pageSize: 10 // 每页条数
      }
      this.getScheduling()
    },
    // 工作模式列表
    async getWorkPatternList() {
      const res = await getWorkPatternListAPI()
      this.workPatternList = res[1]
    },
    // 表格手动选择事件
    onselect(selection, row) {
      this.binding.push(row.id)
    },
    // 绑定排班按钮
    bindingArrange() {
      if (!this.binding.length) {
        this.$message.error('请选择调整人员')
      } else {
        this.dialogVisible = true
      }
    },
    confirmDialog() {
      this.$message.error('很抱歉，演示系统，不允许修改')
      setTimeout(() => {
        this.$message.error('接口异常')
      }, 1500)
    },
    closeDialog() {
      this.dialogVisible = false
    },

    // 分页函数
    handleSizeChange(val) {
      this.pageParams.pageSize = val
      this.getScheduling()
    },
    handleCurrentChange(val) {
      this.pageParams.page = val
      this.getScheduling()
    }
  }
}

</script>
<style scoped lang="scss">

.common{
  background-color: #f4f6f8;
  padding:20px ;
}
.form-box{
  background-color: #ffffff;

  padding:20px 30px;

}
.table{
  margin-top: 20px;

}

</style>
